package com.ly.lypopupwindowdemo.activity;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bm.library.PhotoView;
import com.ly.lypopupwindowdemo.R;
import com.ly.lypopupwindowdemo.pagerTrans.ScaleTransformer;

import me.relex.circleindicator.CircleIndicator;

public class CircleIndicatorDemo extends AppCompatActivity {

    private int[] mImageIds = new int[]{
            R.mipmap.bj3_big, R.mipmap.bj2_big, R.mipmap.bj1_big, R.mipmap.bj4_big
    };
    private CircleIndicator indicator;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_circle_indicator_demo);

        ViewPager viewpager = (ViewPager) findViewById(R.id.viewpager);
        indicator = (CircleIndicator) findViewById(R.id.indicator);



        viewpager.setPageMargin((int) (getResources().getDisplayMetrics().density * 15));
        viewpager.setAdapter(new MyPagerAdaptr());
        indicator.setViewPager(viewpager);
        viewpager.setCurrentItem(1);
        viewpager.setPageTransformer(true, new ScaleTransformer());
    }

    private class MyPagerAdaptr extends PagerAdapter {

        @Override
        public int getCount() {
            return mImageIds.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            //永远这么写
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            PhotoView view = new PhotoView(CircleIndicatorDemo.this);
            view.enable();
            view.setScaleType(ImageView.ScaleType.FIT_CENTER);
            view.setBackgroundResource(mImageIds[position]);
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            //永远这么写
            container.removeView((View) object);
        }
    }
}
